<?php

/**
 * @author    1054229380@qq.com
 * @since    2017-7-3
 * @desc 我上传的文件
 * */
class myUpFile
{

    function __construct()
    {
        $dbTable_prefix = "lf_";
        $dbTable_name = "myupfile";
        $this->tableName = $dbTable_prefix . $dbTable_name;
    }

    /**
     * 添加上传我的文件
     * */
    function addMyFile($myFileInfo)
    {
        $tableName = $this->tableName;
        $upFrom = $myFileInfo['upFrom']; //后台上传
        $infoType = $myFileInfo['infoType']; //商品
        $infoId = $myFileInfo['infoId']; //商品ID
        $fileType = $myFileInfo['fileType']; //0图片1其他
        $imgArr = $myFileInfo['imgArr']; //图片信息

        for ($i = 1; $i < count($imgArr); $i++) {
            $_tmpUrl = $imgArr[$i];
            $dbData['upFrom'] = $upFrom;
            $dbData['infoType'] = $infoType;
            $dbData['infoId'] = $infoId;
            $dbData['fileType'] = $fileType;
            $dbData['createTime'] = time();
            @db::update($tableName, $dbData, "where createTime=0 and fileUrl='$_tmpUrl'");
        }
    }

    /**
     * 添加上传图片
     * author OuPeng
     */
    function addUpFile($authInfo)
    {
        $tabName = $this->tableName;
        $sql = "INSERT into $tabName (upFrom,infoType,infoId,fileType,fileUrl,createTime) VALUES " . $authInfo;
        if (db::runsql($sql)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过ID查询数组
     */
    function getImgListById($id)
    {
        global $myPDO;
        $sql = "select fileURL from cj_myupfile where infoId = $id";
        $listArr = $myPDO->getAll($sql);
        return $listArr;
    }

    /**
     * 删除上传的文件
     * @infoType 1单页,2文章,3商品
     * @delType 0,指定分类及ID的图片,1指定分类过期图片 24小时
     * */
    function delMyFile($infoType, $infoId, $delType = '0')
    {
        if ($infoType == '' && $infoId == '') {
            return false;
        }
        $sear[] = "infoType=$infoType";
        $sear[] = "infoId=$infoId";
        $whereCondition = db::convertSearchToSql($sear);
        $tableName = $this->tableName;
        $delImgArr = db::getArr(array(
            "tableName" => $tableName,
            "selectField" => "id,fileUrl",
            "whereCondition" => $whereCondition
        ));
        if ($whereCondition == "") {
            return false;
        }
        $sql = "delete from {$tableName} {$whereCondition}";
        if (db::runsql($sql)) {
            for ($i = 0; $i < count($delImgArr); $i++) {
                $_delFileUrl = $delImgArr[$i]['fileUrl'];
                @unlink(APP_ROOT . $_delFileUrl);
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过id删除数据
     * @param type $id
     */
    function deleteById($id)
    {
        $tableName = $this->tableName;
        $sql = "delete from $tableName where id = $id";
        if (db::runsql($sql)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 删除上传的文件
     * @infoType 1单页,2文章,3商品
     * @delType 0,指定分类及ID的图片,1指定分类过期图片 24小时
     * */
    function delByFileId($infoType, $infoId, $delType = '0')
    {
        if ($infoType == '' && $infoId == '') {
            return false;
        }
        $sear[] = "infoType=$infoType";
        $sear[] = "infoId=$infoId";
        $whereCondition = db::convertSearchToSql($sear);
        $tableName = $this->tableName;
        $delImgArr = db::getArr(array(
            "tableName" => $tableName,
            "selectField" => "id,fileUrl",
            "whereCondition" => $whereCondition
        ));
        if ($whereCondition == "") {
            return false;
        }
        $sql = "delete from {$tableName} {$whereCondition}";
        if (db::runsql($sql)) {
            for ($i = 0; $i < count($delImgArr); $i++) {
                $_delFileUrl = $delImgArr[$i]['fileUrl'];
                @unlink(APP_ROOT . $_delFileUrl);
            }
            return true;
        } else {
            return false;
        }
    }

}

?>